//
//  ServiceViewController.m
//  Project two framework
//
//  Created by 张峥 on 16/7/21.
//  Copyright © 2016年 张峥. All rights reserved.
//

#import "ServiceViewController.h"
#import "policyConsultView.h"
#import "ComboView.h"
#import "OptionalView.h"
#import "ZPZNetRequest+MealsList.h"
#import "ZPZNetRequest.h"
#import "LStringUtils.h"
#import "AFNetworking.h"
#import <AVKit/AVKit.h>
#import <AVFoundation/AVFoundation.h>
#import "ZPZNetRequest+MealsList.h"

@interface ServiceViewController ()<UITextFieldDelegate>
{
    UIButton *_headLine;
    UIButton *_keyword;
    UIView *_grabbleBtnView;

}

@property(nonatomic,strong)policyConsultView            *policyConsultView;
@property(nonatomic,strong)ComboView                    *comboView;
@property(nonatomic,strong)OptionalView                 *optionalView;


/**分段控制器*/
@property(nonatomic,strong)UISegmentedControl           *segmented;
@property (nonatomic, assign) NSInteger                 lastSelectedIndex;
@property (nonatomic, strong) NSMutableArray            *photoArray;
/**阴影*/
@property(nonatomic,strong)UIImageView                  *shadow;

@property(nonatomic,strong)NSArray                      *imageArray;
@property(nonatomic,strong)NSArray                      *imageArray1;
/**右上角搜索*/
@property(nonatomic,strong)UIView                       *grabbleView;
@property(nonatomic,strong)UIButton                     *grabbleBtn;
@property(nonatomic,strong)UITextField                  *grabbleField;
@property(nonatomic,strong)UIButton                     *grabbleBtn1;

@property(nonatomic,strong)UIView                       *clickGrabbleBtnView;

/**右上角视频链接*/
@property(nonatomic,strong)UIButton                     *interlinkageBtn;
@property(nonatomic,strong)NSString                     *urlString;
//视屏播放器
@property(nonatomic,strong)AVPlayerViewController       *playerViewController;
@end

@implementation ServiceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor yellowColor];
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"背景.png"]];
 
    [self.view addSubview:self.segmented];
    
    [self clickSegmentde:self.segmented];
    [self shadow];

    [self grabbleView];
  
    [self grabbleBtn1];
    [self grabbleField];
    [self grabbleBtn];
    
    [self interlinkageBtn];
    
    
}
//分段控制器的点击方法
-(void)clickSegmentde:(UISegmentedControl *)sender{
    [_segmented setImage:[_imageArray[_lastSelectedIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:_lastSelectedIndex];
    if (sender.selectedSegmentIndex == 0) {
        [_segmented setImage:[_photoArray[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
        [self.view addSubview:self.policyConsultView];
        [self.view addSubview:self.grabbleView];
        _grabbleView.hidden = NO;
        [_optionalView removeFromSuperview];
        [_comboView removeFromSuperview];
    }else if (sender.selectedSegmentIndex == 1){
        [_segmented setImage:[_photoArray[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
        [self.view addSubview:self.comboView];
        [self.view addSubview:_interlinkageBtn];
        _interlinkageBtn.hidden = NO;
        _grabbleView.hidden = YES;
        [_optionalView removeFromSuperview];
        [_policyConsultView removeFromSuperview];
        
    }else if (sender.selectedSegmentIndex == 2){
        [_segmented setImage:[_photoArray[sender.selectedSegmentIndex] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:2];
        [self.view addSubview:self.optionalView];
        [self.view addSubview:_interlinkageBtn];
        _interlinkageBtn.hidden = NO;
        _grabbleView.hidden = YES;
        [_policyConsultView removeFromSuperview];
        [_comboView removeFromSuperview];
        
    }
    _lastSelectedIndex = sender.selectedSegmentIndex;
}
#pragma mark --点击事件
-(void)clickGrabbleBtn{
    NSLog(@"政策质询按钮");
    if (self.clickGrabbleBtnView.hidden) {
        [UIView animateWithDuration:0.5 animations:^{
            [self.view addSubview: _clickGrabbleBtnView];
             self.clickGrabbleBtnView.hidden = NO;
        }];
       
    }else{
        [UIView animateWithDuration:0.5 animations:^{
//            [self clickGrabbleBtnView];
            self.clickGrabbleBtnView.hidden = YES;
            [_clickGrabbleBtnView removeFromSuperview];
        }];
    }
}

//按照标题搜索
-(void)clickHeadline{
    _headLine.backgroundColor = [UIColor colorWithRed:214/250.0 green:214/250.0 blue:214/250.0 alpha:1];
    self.clickGrabbleBtnView.hidden = YES;
    
}

//按照关键字搜索
-(void)clickKeyword{
    _keyword.backgroundColor = [UIColor colorWithRed:214/250.0 green:214/250.0 blue:214/250.0 alpha:1];
    self.clickGrabbleBtnView.hidden = YES;
}

//视频链接按钮
-(void)clickInterlinkageBtn:(UIButton *)sender{
    NSLog(@"视屏链接按钮");
    
    [ZPZNetRequest getVideoComletion:^(id object, BOOL success) {
//                NSLog(@"%@",object);
        if (!success) {
            return ;
        }
        NSDictionary *dic = [[NSDictionary alloc]initWithDictionary:object[@"content"][0]];
        _urlString = [NSString stringWithFormat:@"%@%@",@"http://nmzh.rimiedu.com",dic[@"filename"]];
//        NSLog(@"123321123332123123231213312%@",_urlString);
        //初始化视频播放器
        _playerViewController = [[AVPlayerViewController alloc]init];
        _playerViewController.view.bounds = self.view.bounds;
        _playerViewController.view.center = self.view.center;
        //构建播放地址
        //    NSString *urlStr = @"http://www.iqiyi.com/w_19rr26oia9.html";
        NSURL *url = [NSURL URLWithString:_urlString];
        _playerViewController.player = [AVPlayer playerWithURL:url];
        [self presentViewController:_playerViewController animated:YES completion:^{
            [_playerViewController.player play];
        }];
        
    }];
}

#pragma mark --UITextfield的响应事件
//-(void)textFIeldDidChange:(UITextField *)TextField{
//    if (![TextField.text isEqualToString:@""]) {
//
//        
//    }
//    
//    
//}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    //发送通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"12" object:nil userInfo:@{@"textField":textField.text}];
    return YES;
}

-(ComboView *)comboView
{
    if (_comboView == nil) {
        _comboView = [[ComboView alloc]initWithFrame:CGRectMake(70, 160, SCREEN_WIDTH -70, SCREEN_HEIGHT)];
    }
    return _comboView;
}

-(policyConsultView *)policyConsultView
{
    if (_policyConsultView == nil) {
        _policyConsultView = [[policyConsultView alloc]initWithFrame:CGRectMake(45, 0, 825, 480)];
    }
    return _policyConsultView;
}
-(OptionalView *)optionalView
{
    if (_optionalView == nil) {
        _optionalView = [[OptionalView alloc]initWithFrame:CGRectMake(70, 160, SCREEN_WIDTH -70, SCREEN_HEIGHT)];
    }
    return _optionalView;
}

-(UIImageView *)shadow
{
    if (_shadow == nil) {
        _shadow = [[UIImageView alloc]initWithFrame:CGRectMake(SCREEN_WIDTH/2/2 +5, 65, 385, 12)];
        _shadow.image = [UIImage imageNamed:@"shadow_08.png"];
        [self.view addSubview:_shadow];
    }
    return _shadow;
}

-(UISegmentedControl *)segmented
{
    if (_segmented == nil) {
        UIImage * image1=[UIImage imageNamed:@"a_07.png"];
        UIImage * image2=[UIImage imageNamed:@"b_08.png"];
        UIImage * image3=[UIImage imageNamed:@"c_09.png"];
        UIImage * image4=[UIImage imageNamed:@"aa_07.png"];
        UIImage * image5=[UIImage imageNamed:@"bb_08.png"];
        UIImage * image6=[UIImage imageNamed:@"cc_09.png"];
        _imageArray = [[NSArray alloc]initWithObjects:image1,image2,image3, nil];
        _photoArray = [[NSMutableArray alloc]initWithObjects:image4,image5,image6, nil];
        _segmented = [[UISegmentedControl alloc]initWithItems:@[@"",@"",@""]];
        //        //必须把图片等渲染方式改变了，不然会不起作用
        //        [_segmented setImage:[image1 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:0];
        //        [_segmented setImage:[image2 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:1];
        //        [_segmented setImage:[image3 imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:2];
        _segmented.frame = CGRectMake(SCREEN_WIDTH/2/2, 25, 390, 40);
        for (NSInteger i = 0; i < 3; i++) {
            if (i == 0) {
                [_segmented setImage:[_photoArray[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:i
                 ];
            }else{
                [_segmented setImage:[_imageArray[i] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forSegmentAtIndex:i
                 ];
            }
        }
        
        
        _segmented.tintColor = [UIColor clearColor];
        //默认第一个
        _segmented.selectedSegmentIndex = 0;
        _segmented.layer.cornerRadius = 20;
        _segmented.clipsToBounds = YES;
        [_segmented addTarget:self action:@selector(clickSegmentde:) forControlEvents:UIControlEventValueChanged];
    }
    _lastSelectedIndex = 0;
    return _segmented;
}

-(UIView *)grabbleView
{
    if (_grabbleView == nil) {
        _grabbleView = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 + 150, 25, 240, 40)];
        _grabbleView.backgroundColor = [UIColor clearColor];
        _grabbleView.hidden = YES;
        [self.view addSubview:_grabbleView];
        
    }
    return _grabbleView;

}

-(UIButton *)grabbleBtn
{
    if (_grabbleBtn == nil) {
        _grabbleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _grabbleBtn.frame = CGRectMake(0, 0, 56, 40);
        [_grabbleBtn setImage:[UIImage imageNamed:@"choose_01"] forState:UIControlStateNormal];
        [_grabbleBtn addTarget:self action:@selector(clickGrabbleBtn) forControlEvents:UIControlEventTouchUpInside];
        _grabbleBtn.userInteractionEnabled = YES;
        [_grabbleBtn1 addSubview:_grabbleBtn];
        
        
    }
    return _grabbleBtn;
}

-(UITextField *)grabbleField
{
    if (_grabbleField == nil) {
        _grabbleField = [[UITextField alloc]initWithFrame:CGRectMake(60, 0, 180, 40)];
        _grabbleField.backgroundColor = [UIColor clearColor];
        _grabbleField.textAlignment = NSTextAlignmentLeft;
        _grabbleField.clearButtonMode = UITextFieldViewModeWhileEditing;
        // textField的文本发生变化时相应事件
//        [_grabbleField addTarget:self action:@selector(textFIeldDidChange:) forControlEvents:UIControlEventValueChanged];
        _grabbleField.delegate = self;
        [_grabbleBtn1 addSubview:_grabbleField];
    }
    return _grabbleField;
}


-(UIButton *)grabbleBtn1{
    if (_grabbleBtn1 == nil) {
        _grabbleBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        _grabbleBtn1.frame = CGRectMake(0, 0, 240, 40);
        [_grabbleBtn1 setImage:[UIImage imageNamed:@"搜索框_11.png"] forState:UIControlStateNormal];
//        _grabbleBtn1.backgroundColor = [UIColor redColor];
        _grabbleBtn1.userInteractionEnabled = YES;
        [_grabbleView addSubview:_grabbleBtn1];
    }
    return _grabbleBtn1;
}


-(UIButton *)interlinkageBtn
{
    if (_interlinkageBtn == nil) {
        _interlinkageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _interlinkageBtn.frame = CGRectMake(700, 25, 50, 40);
        [_interlinkageBtn setImage:[UIImage imageNamed:@"视频图标_05"] forState:UIControlStateNormal];
        _interlinkageBtn.hidden = YES;
        
        [_interlinkageBtn addTarget:self action:@selector(clickInterlinkageBtn:) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_interlinkageBtn];
    }
    return _interlinkageBtn;

}

-(UIView *)clickGrabbleBtnView
{
    if (_clickGrabbleBtnView == nil) {
        _clickGrabbleBtnView = [[UIView alloc]initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,SCREEN_HEIGHT)];
        _clickGrabbleBtnView.backgroundColor = [UIColor clearColor];
//        _clickGrabbleBtnView.alpha = 0.6;
        
        UIView *grabbleBtnView = [[UIView alloc]initWithFrame:CGRectMake(self.view.bounds.size.width / 2 + 200, 65, 240, 160)];
        
        grabbleBtnView.backgroundColor = [UIColor colorWithRed:214/250.0 green:214/250.0 blue:214/250.0 alpha:1];
        grabbleBtnView.alpha = 0.6;
        _grabbleBtnView = grabbleBtnView;
        [_clickGrabbleBtnView addSubview:grabbleBtnView];
        
        UIButton *headline = [UIButton buttonWithType:UIButtonTypeCustom];
        headline.frame = CGRectMake(0, 0, 240, 30);
        headline.backgroundColor = [UIColor whiteColor];
        [headline setTitle:@"标题" forState:UIControlStateNormal];
        [headline setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        headline.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _headLine = headline;
        [headline addTarget:self action:@selector(clickHeadline) forControlEvents:UIControlEventTouchUpInside];
        [grabbleBtnView addSubview:headline];
        
        UIButton *keyword = [UIButton buttonWithType:UIButtonTypeCustom];
        keyword.frame = CGRectMake(0, 30, 240, 30);
        keyword.backgroundColor = [UIColor whiteColor];
        [keyword setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //文字居左
        keyword.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _keyword = keyword;
        [keyword setTitle:@"关键字" forState:UIControlStateNormal];
        [keyword addTarget:self action:@selector(clickKeyword) forControlEvents:UIControlEventTouchUpInside];
        
        [grabbleBtnView addSubview:keyword];
        
        [self.view addSubview:_clickGrabbleBtnView];
    }
    return _clickGrabbleBtnView;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    UITouch *touch = [touches anyObject];
    [touch locationInView:_clickGrabbleBtnView];
    [UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        [_clickGrabbleBtnView removeFromSuperview];
//        _clickGrabbleBtnView.hidden = YES;
        
    } completion:^(BOOL finished) {
        NSLog(@"动画完成");
    }];
    

}



@end
